package com.transcoachapp.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.transcoachapp.BusStations;
import com.transcoachapp.R;
import com.transcoachapp.data.Line;

public class ResultSetAdapter extends ArrayAdapter<ArrayList<Line>> {
	private Activity context;
	private ArrayList<ArrayList<Line>> items;
	
	public ResultSetAdapter(Context context, int textViewResourceId,
			ArrayList<ArrayList<Line>> objects) { 
		super(context, textViewResourceId,objects);
		this.items = objects;
		this.context = (Activity) context;
	//	dbh = new DataBaseHelper(context);
	//	db=dbh.getReadableDatabase();
	//	dbAccess();
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (this.items != null)
		      return this.items.size();
		return 0;
	}
	@Override
	public ArrayList<Line> getItem(int arg0) {
		// TODO Auto-generated method stub
		return this.items.get(arg0);
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{   // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
		View bsdRow = null;
		TextView text;
		final ArrayList<Line> bs  = getItem(position);
		if(bs.size()==1){
			bsdRow = context.getLayoutInflater().inflate(R.layout.result_line, null);
			text = (TextView) bsdRow.findViewById(R.id.text1);
			text.setText(bs.get(0).getLineCode());
			
		}
		else if(bs.size()==2){
			bsdRow = context.getLayoutInflater().inflate(R.layout.result_line2, null);
			text = (TextView) bsdRow.findViewById(R.id.text1);
			text.setText(bs.get(0).getLineCode()+"  -  "+bs.get(1).getLineCode());
		}
		else if(bs.size()==3){
			bsdRow = context.getLayoutInflater().inflate(R.layout.result_line3, null);
			text = (TextView) bsdRow.findViewById(R.id.text1);
			text.setText(bs.get(0).getLineCode()+"  -  "+bs.get(1).getLineCode()+"  -  "+bs.get(2).getLineCode());
		}
		
		TextView textView = (TextView) bsdRow.findViewById(R.id.textView1);
        textView.setText(bs.get(0).getOrder()+"- "+bs.get(0).getLineCode()+":");
        TextView textView2 = (TextView) bsdRow.findViewById(R.id.textView2);
        textView2.setText(bs.get(0).getStartPoint());
        TextView textView3 = (TextView) bsdRow.findViewById(R.id.textView3);
        textView3.setText(bs.get(0).getEndPoint());
        
        TextView view1 = (TextView) bsdRow.findViewById(R.id.view1);
        view1.setText(bs.get(0).getMinutes()+" minute");
	    TextView view2 = (TextView) bsdRow.findViewById(R.id.view2);
	    view2.setText(bs.get(0).getStationCount() +" stations");
	    
        
        if(bs.size()>1){
    		TextView textView4 = (TextView) bsdRow.findViewById(R.id.textView4);
            textView4.setText(bs.get(1).getOrder()+"- "+bs.get(1).getLineCode()+":");
            TextView textView5 = (TextView) bsdRow.findViewById(R.id.textView5);
            textView5.setText(bs.get(1).getStartPoint());
            TextView textView6 = (TextView) bsdRow.findViewById(R.id.textView6);
            textView6.setText(bs.get(1).getEndPoint());
            
            
          
            
            TextView view3 = (TextView) bsdRow.findViewById(R.id.view3);
            view3.setText(bs.get(1).getMinutes()+" minute");
		    TextView view4 = (TextView) bsdRow.findViewById(R.id.view4);
		    view4.setText(bs.get(1).getStationCount() +" stations");
		    
        }
        if(bs.size()==3){
    		TextView textView7 = (TextView) bsdRow.findViewById(R.id.textView7);
            textView7.setText(bs.get(2).getOrder()+"- "+bs.get(2).getLineCode()+":");
            TextView textView8 = (TextView) bsdRow.findViewById(R.id.textView8);
            textView8.setText(bs.get(2).getStartPoint());
            TextView textView9 = (TextView) bsdRow.findViewById(R.id.textView9);
            textView9.setText(bs.get(2).getEndPoint());
            
            TextView view5 = (TextView) bsdRow.findViewById(R.id.view5);
            view5.setText(bs.get(2).getMinutes()+" minute");
		    TextView view6 = (TextView) bsdRow.findViewById(R.id.view6);
		    view6.setText(bs.get(2).getStationCount() +" stations");
            
        }
		/*
	    bsdRow.setClickable(true);
		bsdRow.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				Intent intent = new Intent();
                intent.setClass(context, BusStations.class);
                intent.putExtra("type", bs.getType());
                intent.putExtra("routeCode", bs.getRouteCode());
                intent.putExtra("routeName", bs.getRouteName());
				context.startActivity(intent);	
			}
		});
*/
		return bsdRow;
	}
}
